﻿package com.tudou.ad.view
{
    import com.tudou.ad.components.events.*;
    import com.tudou.ad.view.base.*;
    import com.tudou.home.consts.*;
    import com.tudou.skin.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class AdOverlayView extends AdItemViewBase
    {
        protected var closeBtn:InteractiveObject;
        protected var timer:Timer;
        public static const CLOSE_TIME:uint = 15;

        public function AdOverlayView()
        {
            return;
        }// end function

        protected function initAutoClose() : void
        {
            timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER, timerEventHlr);
            return;
        }// end function

        protected function clearAutoClose() : void
        {
            if (timer)
            {
                timer.removeEventListener(TimerEvent.TIMER, timerEventHlr);
            }
            timer = null;
            return;
        }// end function

        protected function timerEventHlr(event:TimerEvent) : void
        {
            var _loc_2:int = 0;
            _loc_2 = Math.max(0, int(_countDownTime) - timer.currentCount);
            if (timer.currentCount >= CLOSE_TIME)
            {
                adShowComplete();
            }
            return;
        }// end function

        override public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            _isFullscreen = param3;
            if (frontAdSprite)
            {
                frontAdSprite.width = param1;
            }
            if (closeBtn)
            {
                closeBtn.x = param1 - closeBtn.width - 4;
                closeBtn.y = 4;
            }
            return;
        }// end function

        protected function closeHlr(event:Event) : void
        {
            _adShowUserSkip();
            clearAd();
            return;
        }// end function

        override public function clearAd() : void
        {
            _clearAd(true);
            return;
        }// end function

        protected function _clearAd(param1:Boolean) : void
        {
            if (!clearAdCommon())
            {
                return;
            }
            isSkip = param1;
            if (isSkip)
            {
                _adShowSkip();
            }
            hideAd();
            clearComps();
            countDownTime = 0;
            adItemInfo = null;
            adShowEnd();
            return;
        }// end function

        override public function startAd() : void
        {
            if (!startAdCommon())
            {
                return;
            }
            showAd();
            frontAdSprite.visible = true;
            timer.reset();
            timer.start();
            adShowStart();
            return;
        }// end function

        protected function initCloseBtn() : void
        {
            closeBtn = new CloseBTN();
            viewBox.addChild(closeBtn);
            closeBtn.addEventListener(MouseEvent.CLICK, closeHlr);
            return;
        }// end function

        override public function initToShow() : void
        {
            if (!initToShowCommon())
            {
                return;
            }
            hasFront = adItemInfo.url ? (true) : (false);
            initFrontStuff();
            initAutoClose();
            initCloseBtn();
            addToShowStuff();
            return;
        }// end function

        protected function clearCloseBtn() : void
        {
            if (closeBtn)
            {
                closeBtn.removeEventListener(MouseEvent.CLICK, closeHlr);
                viewBox.removeChild(closeBtn);
            }
            closeBtn = null;
            return;
        }// end function

        override protected function adShowComplete() : void
        {
            if (!isSkip)
            {
                _adShowComplete();
            }
            _clearAd(isSkip);
            return;
        }// end function

        override protected function initFrontStuff() : void
        {
            frontAdSprite = new OverlayAdSprite(AdSize.OVERLAY_WIDTH, AdSize.OVERLAY_HEIGHT, 16711680, 0);
            viewBox.addChild(frontAdSprite);
            frontAdSprite.addEventListener(TudouAdSpriteEvent.COMPLETE, swfCompleteHlr);
            frontAdSprite.addEventListener(TudouAdSpriteEvent.CLICK, frontStuffClickHlr);
            return;
        }// end function

        override protected function adClick(param1:String = null) : void
        {
            if (param1 == "")
            {
                openURL(adItemInfo.link);
            }
            _adClick(param1);
            return;
        }// end function

        override protected function clearComps() : void
        {
            clearFrontStuff();
            clearAutoClose();
            clearCloseBtn();
            clearCompsCommon();
            return;
        }// end function

    }
}

class OverlayAdSprite extends TudouAdSprite
{

    function OverlayAdSprite(param1:Number, param2:Number, param3:uint = 0, param4:Number = 0)
    {
        super(param1, param2, param3, param4);
        return;
    }// end function

    override public function setWidthHeight(param1:Number, param2:Number) : void
    {
        background.width = param1;
        background.height = param2;
        this.mask.width = param1;
        this.mask.height = param2;
        adDisplay.width = param1;
        adDisplay.height = param2;
        return;
    }// end function

}

